package logic

import (
	"context"
	"rent/models"

	"rent/rentrpc/internal/svc"
	"rent/rentrpc/rentrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectListLogic {
	return &CollectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectListLogic) CollectList(in *rentrpc.CollectListRequest) (*rentrpc.CollectListResponse, error) {
	// todo: add your logic here and delete this line
	var collect []models.CommodityData
	models.DB.Model(&models.Collect{}).
		Select(" commodities.*,  users.image,  users.user_name").
		Joins(" LEFT JOIN commodities ON commodities.id = collects.commodity_id").
		Joins(" LEFT JOIN users on commodities.user_id=users.id").
		Where("collects.user_id=?", in.UserId).Debug().
		Find(&collect)
	var collectDatas []*rentrpc.CollectData
	for _, data := range collect {
		collectData := rentrpc.CollectData{
			Title:     data.Title,
			Class:     data.Class,
			Price:     float32(data.Price),
			Details:   data.Details,
			UserName:  data.UserName,
			UserImage: data.Image,
		}
		collectDatas = append(collectDatas, &collectData)
	}
	return &rentrpc.CollectListResponse{
		List: collectDatas,
	}, nil
}
